package com.party.web.controller.system;

import com.party.common.annotation.Log;
import com.party.common.constant.UserConstants;
import com.party.common.core.controller.BaseController;
import com.party.common.core.domain.AjaxResult;
import com.party.common.core.domain.entity.SysPost;
import com.party.common.enums.BusinessType;
import com.party.common.utils.StringUtils;
import com.party.system.service.ISysPostNewService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/post")
public class SysPostNewController extends BaseController
{
    @Autowired
    private ISysPostNewService   postService;

    /**
     * 获取岗位列表
     */
//    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public AjaxResult list(SysPost post)
    {
        List<SysPost> posts = postService.selectPostList(post);
        return success(posts);
    }



    @GetMapping("/getPostlist")
    public AjaxResult getPostlist(SysPost post)
    {
        List<SysPost> posts = postService.selectPostListQuery(post);
        return success(posts);
    }


    /**
     * 查询岗位列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list/exclude/{postId}")
    public AjaxResult excludeChild(@PathVariable(value = "postId", required = false) Long postId)
    {
        List<SysPost> posts = postService.selectPostList(new SysPost());
        posts.removeIf(d -> d.getPostId().intValue() == postId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), postId + ""));
        return success(posts);
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable Long postId)
    {
//        postService.checkPostDataScope(postId);
        return success(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysPost post)
    {
        if (!postService.checkPostNameUnique(post))
        {
            return error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }

//        if (!postService.checkPostNoUnique(post))
//        {
//            return error("新增岗位'" + post.getPostCode() + "'失败，岗位编号已存在");
//        }

        post.setCreateBy(getUsername());
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysPost post)
    {
        Long postId = post.getPostId();
//        postService.checkPostDataScope(postId);
        if (!postService.checkPostNameUnique(post))
        {
            return error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (postId.equals(post.getParentId()))
        {
            return error("修改岗位'" + post.getPostName() + "'失败，上级岗位不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, post.getStatus()) && postService.selectNormalChildrenPostById(postId) > 0)
        {
            return error("该岗位包含未停用的子岗位！");
        }
//        if (!postService.checkPostNoUnique(post))
//        {
//            return error("修改岗位'" + post.getPostCode() + "'失败，岗位编号已存在");
//        }
        post.setUpdateBy(getUsername());
        return toAjax(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postId}")
    public AjaxResult remove(@PathVariable Long postId)
    {
        if (postService.hasChildByPostId(postId))
        {
            return warn("存在下级岗位,不允许删除");
        }
//        if (postService.checkPostExistUser(postId))
//        {
//            return warn("岗位存在用户,不允许删除");
//        }
//        postService.checkPostDataScope(postId);
        return toAjax(postService.deletePostById(postId));
    }
}
